package by.amakarevich.services.impl;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import by.amakarevich.dataaccess.NeedleDao;
import by.amakarevich.dataaccess.ProductDao;
import by.amakarevich.datamodel.Needle;
import by.amakarevich.datamodel.Product;
import by.amakarevich.services.NeedleService;

@Service
public class NeedleServiceImpl implements NeedleService {
	@Inject
	private NeedleDao needleDao;
	
	@Inject
	private ProductDao productDao;
	
	@Override
	public void createNewNeedle(Needle needle,  Product product) {
		Validate.isTrue(needle.getId() == null, "This method should be called for 'not saved yet' profile only. Use UPDATE instead");
		needle.setProduct(product);
		needleDao.insert(needle);
	}

	@Override
	public Needle get(Long id) {
		return needleDao.getById(id);
	}

	@Override
	public void updateNeedle(Needle needle) {
		needleDao.update(needle);
	}

	@Override
	public void removeNeedle(Long id) {

		needleDao.delete(id);
		productDao.delete(id);

	}
	  @Override
	    public void deleteAll() {
		  needleDao.deleteAll();
		  productDao.deleteAll();
	    }
}
